From b271a94f927c2b7b0c42e58be61d6fdb44480b99 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Fri, 17 Sep 2021 03:25:35 +0200 Subject: [PATCH] texture: Rework error enum 1. Change INSUFFICIENT_MEMORY to TOO_LARGE GTK crashes on insufficient memory, we don't emit GErrors. 2. Split UNSUPPORTED into UNSUPPORTED_CONTENT and UNSUPPORTED_FORMAT So we know if you need to find an RPM with a loader or curse and the weird file. 3. Translate error messages, they are meant for end users. --- gdk/gdktexture.h | 13 +++++++++---- gdk/loaders/gdkjpeg.c | 18 +++++++++--------- gdk/loaders/gdkpng.c | 30 ++++++++++-------------------- gdk/loaders/gdktiff.c | 12 +++++++----- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/gdk/gdktexture.h b/gdk/gdktexture.h index aba864e39f..6a62c9fe50 100644 --- a/gdk/gdktexture.h +++ b/gdk/gdktexture.h @@ -45,17 +45,22 @@ GQuark gdk_texture_error_quark (void); /** * GdkTextureError: - * @GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY: Not enough memory to handle this image + * @GDK_TEXTURE_ERROR_TOO_LARGE: Not enough memory to handle this image * @GDK_TEXTURE_ERROR_CORRUPT_IMAGE: The image data appears corrupted - * @GDK_TEXTURE_ERROR_UNSUPPORTED: The image format is not supported + * @GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT: The image contains features + * that cannot be loaded + * @GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT: The image format is not supported * * Possible errors that can be returned by `GdkTexture` constructors. + * + * Since: 4.6 */ typedef enum { - GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, + GDK_TEXTURE_ERROR_TOO_LARGE, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, - GDK_TEXTURE_ERROR_UNSUPPORTED, + GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT, + GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT, } GdkTextureError; GDK_AVAILABLE_IN_ALL diff --git a/gdk/loaders/gdkjpeg.c b/gdk/loaders/gdkjpeg.c index 506a278aaa..c64a0ea99d 100644 --- a/gdk/loaders/gdkjpeg.c +++ b/gdk/loaders/gdkjpeg.c @@ -19,8 +19,10 @@ #include "gdkjpegprivate.h" +#include "gdkintl.h" #include "gdktexture.h" #include "gdkmemorytextureprivate.h" + #include #include #include @@ -53,10 +55,8 @@ fatal_error_handler (j_common_ptr cinfo) if (errmgr->error && *errmgr->error == NULL) g_set_error (errmgr->error, GDK_TEXTURE_ERROR, - cinfo->err->msg_code == JERR_OUT_OF_MEMORY - ? GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY - : GDK_TEXTURE_ERROR_CORRUPT_IMAGE, - "Error interpreting JPEG image file (%s)", buffer); + GDK_TEXTURE_ERROR_CORRUPT_IMAGE, + _("Error interpreting JPEG image file (%s)"), buffer); siglongjmp (errmgr->setjmp_buffer, 1); @@ -213,17 +213,17 @@ gdk_load_jpeg (GBytes *input_bytes, break; default: g_set_error (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED, - "Unsupported colorspace in jpeg (%d)", info.out_color_space); + GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT, + _("Unsupported JPEG colorspace (%d)"), info.out_color_space); jpeg_destroy_decompress (&info); return NULL; } if (!data) { - g_set_error_literal (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, - "Not enough memory to load jpeg"); + g_set_error (error, + GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE, + _("Not enough memory for image size %ux%u"), width, height); jpeg_destroy_decompress (&info); return NULL; } diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c index fa0258b0ba..80fca2460b 100644 --- a/gdk/loaders/gdkpng.c +++ b/gdk/loaders/gdkpng.c @@ -19,9 +19,10 @@ #include "gdkpngprivate.h" +#include "gdkintl.h" +#include "gdkmemorytextureprivate.h" #include "gdktexture.h" #include "gdktextureprivate.h" -#include "gdkmemorytextureprivate.h" #include "gsk/ngl/fp16private.h" #include #include @@ -113,7 +114,7 @@ png_simple_error_callback (png_structp png, if (error && !*error) g_set_error (error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, - "Error reading png (%s)", error_msg); + _("Error reading png (%s)"), error_msg); longjmp (png_jmpbuf (png), 1); } @@ -320,22 +321,11 @@ gdk_load_png (GBytes *bytes, png_malloc_callback, png_free_callback); if (png == NULL) - { - g_set_error (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, - "Failed to parse png image"); - return NULL; - } + g_error ("Out of memory"); info = png_create_info_struct (png); if (info == NULL) - { - png_destroy_read_struct (&png, NULL, NULL); - g_set_error (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, - "Failed to parse png image"); - return NULL; - } + g_error ("Out of memory"); png_set_read_fn (png, &io, png_read_func); @@ -385,8 +375,8 @@ gdk_load_png (GBytes *bytes, { png_destroy_read_struct (&png, &info, NULL); g_set_error (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED, - "Failed to parse png image"); + GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT, + _("Failed to parse png image")); return NULL; } @@ -437,9 +427,9 @@ gdk_load_png (GBytes *bytes, g_free (buffer); g_free (row_pointers); png_destroy_read_struct (&png, &info, NULL); - g_set_error_literal (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, - "Not enough memory to load png"); + g_set_error (error, + GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE, + _("Not enough memory for image size %ux%u"), width, height); return NULL; } diff --git a/gdk/loaders/gdktiff.c b/gdk/loaders/gdktiff.c index 6f41811791..62d2194f0c 100644 --- a/gdk/loaders/gdktiff.c +++ b/gdk/loaders/gdktiff.c @@ -19,9 +19,11 @@ #include "gdktiffprivate.h" +#include "gdkintl.h" +#include "gdkmemorytextureprivate.h" #include "gdktexture.h" #include "gdktextureprivate.h" -#include "gdkmemorytextureprivate.h" + #include /* Our main interest in tiff as an image format is that it is @@ -374,7 +376,7 @@ load_fallback (TIFF *tif, { g_set_error_literal (error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, - "Failed to load RGB data from TIFF file"); + _("Failed to load RGB data from TIFF file")); g_free (data); return NULL; } @@ -471,8 +473,8 @@ gdk_load_tiff (GBytes *input_bytes, if (!data) { g_set_error (error, - GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_INSUFFICIENT_MEMORY, - "Not enough memory to read tiff"); + GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE, + _("Not enough memory for image size %ux%u"), width, height); TIFFClose (tif); return NULL; } @@ -484,7 +486,7 @@ gdk_load_tiff (GBytes *input_bytes, { g_set_error (error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE, - "Reading data failed at row %d", y); + _("Reading data failed at row %d"), y); TIFFClose (tif); g_free (data); return NULL; -- 2.30.2